#include <gps_gsm_sim908.h>

// Product name: GPS/GPRS/GSM Module V3.0
// # Product SKU : TEL0051
// # Version     : 0.1

// # Description:
// # The sketch for driving the gsm mode via the Arduino board

// # Steps:
// #        1. Turn the S1 switch to the Prog(right side)
// #        2. Turn the S2 switch to the Arduino side(left side)
// #        3. Take off the GSM/GPS jumper caps from the Uart select
// #        4. Upload the sketch to the Arduino board
// #        5. Turn the S1 switch to the comm(left side)
// #        6. Plug the jumper caps back to GSM side
// #        7. RST the board

// #        wiki link- http://www.dfrobot.com/wiki/index.php/GPS/GPRS/GSM_Module_V3.0_(SKU:TEL0051)
#include <SoftwareSerial.h>
#include "sms.h"
//#include "gps_gsm_sim908.h"

#define gps_enable()    digitalWrite (4, LOW)
#define gps_disable()   digitalWrite (4, HIGH)
 
#define gsm_enable()    digitalWrite (3, LOW)
#define gsm_disable()   digitalWrite (3, HIGH)

SMSGSM sms;

byte gsmDriverPin[3] = {
  3,4,5};//The default digital driver pins for the GSM and GPS mode
//If you want to change the digital driver pins
//or you have a conflict with D3~D5 on Arduino board,
//you can remove the J10~J12 jumpers to reconnect other driver pins for the module!

/**********************************************
 Functions created by me
***********************************************/
  int BufferIndex = 0;
//function to send a message
void sendMessage(char* message){
  Serial.println("AT+CMGF=1");
  delay(1000);
  Serial.println("AT+CMGS=\"14692545597\"");//Change the receiver phone number
  delay(1000);
  Serial.print(message);
  delay(1000);
  Serial.write(26);
}
//------------
//function to read text messages
/* Inputs:
 int stat: 0 to read all unread
 1 to read read
 2 to read ALL
 boolean markAsRead: True if message when message is read, it turns into "read" status
 //------------*/
void readMessages(int stat, boolean markAsRead=true){
  delay(100);
  int index = 0;
  String command = "AT+CMGL=";
  double prevTimer = millis();
  double currTimer = millis();
  
  Serial.println("AT+CMGF=1"); //select configuration "text"
  delay(2000);
  
  if (stat == 0)
    command.concat("\"REC UNREAD\"");
  else if (stat == 1)
    command.concat("\"REC READ\"");
  else if (stat == 2)
    command.concat("\"ALL\"");
  if (markAsRead)
    command.concat(",1");

  //send the command 
  Serial.println(command);
  
}
//------------------------------
//function to fill the buffer
// no inputs so far... maybe specify command to read at a later point?
//------------------------------
void readReply(){
  char replyCommand [7] = {'+','C','M','G','L',':',' '};
  char txtIndex; 
  //read the feedback

  char readChar [100] = { 0 };
  while(Serial.available()){
      readChar[BufferIndex]=Serial.read();
      //compare each read character to each char from replyCommand
      if (readChar[BufferIndex] == replyCommand[BufferIndex]){
        //if index ==6 ; it matches all characters from replyCommand
        if(BufferIndex == 6){
        }
        BufferIndex++;
      }
      else if(BufferIndex == 7){
        txtIndex = readChar[BufferIndex];
        Serial.print("Txt msg rcv. index: ");Serial.println(txtIndex);
        BufferIndex++;
      }
      else{
        BufferIndex = 0;
      }
  }
  //Serial.print(readChar[index]);
  /*delay(10);
  currTimer = millis(); //get time lapsed
  if (currTimer - prevTimer > 10000){//every 10 seconds
    Serial.println(command);
    prevTimer = millis(); //update prev timer
  }*/
}
/*--------------------------------
 When I run on 
 USB mode, enable this code instead of the other one
 void setup()
 {
 //Init the driver pins for GSM function
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 //Output GSM Timing  
 digitalWrite(5,HIGH);
 delay(1500);
 digitalWrite(5,LOW); 
 }
 void loop()    
 { 
 digitalWrite(3,HIGH);//disable GSM TX?RX
 digitalWrite(4,HIGH);//disable GPS TX?RX
 }
 ------------------------------------*/

//------------------------------------------
//setup and loop functions
void setup()
{   
  Serial.begin(9600); //set the baud rate
  //Init the driver pins for GSM function
  /*
  for(int i = 0 ; i < 3; i++){
    pinMode(gsmDriverPin[i],OUTPUT);
  }*/
  gps_init();//sets pins 3,4,5 to OUT mode
  
  digitalWrite(5,HIGH);//Output GSM Timing
  delay(1500);
  digitalWrite(5,LOW); 
  
  gsm_enable (); //pin 3 lo
  gps_disable ();//pin 4 hi
/*digitalWrite(3,LOW);//Enable the GSM mode
  digitalWrite(4,HIGH);//Disable the GPS mode*/

  delay(2000);

  Serial.println("Setup, please wait...");
  delay(5000);//call ready
  //  sendMessage("This is a message");
  delay(10000);
  Serial.println();
  char readChar;
  Serial.println("Dumping");
  while(Serial.available()){
     Serial.read();
  }
  Serial.println(".");
  delay(2000);
  Serial.println("AT"); //Send AT command 
  delay(2000);
  Serial.println("AT+CGPSIPR=9600");
  delay(2000);

}
boolean firstRun = true;
void loop()
{ 
  
    //Send message
  if (firstRun){
    firstRun = false;
    readMessages(2,false);
  }
  delay(100);
  readReply();
  //sendMessage("Message is here)");
//  Serial.println("The End");
//  while(1);
}


